<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MUser extends MModel
{
    
    public static function initUser($user) {
        if ($user === null) {
            return null;
        }
        $userObj = new MUser();
        $userObj->assembleUser($user);
        return $userObj;
    }

    
    public function queryUsersByIdN($id) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_users where id=$id";
        $db_user = $db->selectDb($sql);

        if (empty($db_user)){
            return false;
        }
        return $db_user[0];
    }
    
    
    
    public function queryUsersByID($id) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_users where id=$id";
        $db_user = $db->selectDb($sql);

        if (empty($db_user)){
            return false;
        }
        return self::assembleUser($db_user[0]);
    }


    
    public function queryUserName($user_name) {
        $db = MDbManager::getInstance();
                $sql = "select * from ".DB_PREFIX."_users where user_name=\"{$user_name}\"";

        FXP::trace("queryUserName:".$sql);
        $db_user = $db->selectDb($sql);

        if (empty($db_user)){
            return false;
        }
        return $db_user[0];
    }

    
    public function queryUserByUserName($user_name, $password) {
        $db = MDbManager::getInstance();
                $sql = "select * from ".DB_PREFIX."_users where user_name=\"{$user_name}\" and user_pass=\"{$password}\"";

        FXP::trace("queryUserByUserName:".$sql);
        $db_user = $db->selectDb($sql);

        if (empty($db_user)){
            return false;
        }
        return self::assembleUser($db_user[0]);
    }
    
    public function assembleUser($user_data){
        $this->id             = $user_data["id"];
        $this->user_id        = $user_data["id"];
        $this->user_uuid      = $user_data["user_uuid"];
        $this->user_name      = $user_data["user_name"];
        $this->user_pass      = $user_data["user_pass"];
        $this->user_status    = $user_data["user_status"];
        $this->created_at     = $user_data["created_at"];
        $this->updated_at     = $user_data["updated_at"];
                $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_user_metas where user_id={$this->user_id} and meta_key in('space','phone','email','nick')";
        $items = $db->selectDb($sql);

        $this->nick  = $this->user_name;        $this->phone = "";        $this->email = "";        foreach($items as $index=>$item){
            if($item["meta_key"]=="space"){
                $this->space=$item["meta_value"]*1024*1024;
            }
            if($item["meta_key"]=="nick"){
                $this->nick=$item["meta_value"];
            }
            if($item["meta_key"]=="phone"){
                $this->phone=$item["meta_value"];
            }
            if($item["meta_key"]=="email"){
                $this->email=$item["meta_value"];
            }
        }

                if (empty($this->space)){
            $this->space = MUtils::totalSize($this->user_id);
        }
        
        $this->usedSpace = self::getUsedSpaceById($this->user_id);
        return $this;
    }
    
        public static function getUsedSpaceById($id) {
        $db = MDbManager::getInstance();
        $sql = "select sum(file_size) as usedSpace from ".DB_PREFIX."_files where user_id = " . $id . " and is_deleted=0";
        $items = $db->selectDb($sql);
        $usedSpace=0;
        if (!empty($items) && !is_null($items[0]["usedSpace"])){
            $usedSpace=$items[0]["usedSpace"];
        }
        return $usedSpace;
    }
}
?>